/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * Projects
 * Copyright (C) Arnaud Duval 2010 <arnaud.duval@gmail.com>
 * 
 * Projects is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Projects is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
#include <filesystem-utils.hpp>
#include <config.hpp>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <unistd.h>

 
 
///@brief Create a directory
///@param path Path to the directory to create
void CreateDir(std::string path)
{
    std::string wrongSep;
    if(TRAININGBOOK_FILE_SEPARATOR == "\\")
        wrongSep = "/";
    else
        wrongSep = "\\";
       
    size_t pos = path.find(wrongSep);
    while(pos != std::string::npos)
    {
        std::cout << "je suis la\n";
        path.replace(pos, 1, TRAININGBOOK_FILE_SEPARATOR);
        pos = path.find(wrongSep);
    }
    
	std::string mkdirCommand = std::string(TRAININGBOOK_MKDIR_COMMAND) + " "+ path;
	std::cout << std::system(mkdirCommand.c_str()) << "\n";
}

///@brief Remove a file
///@param path Path to the file to remove
void RemoveFile(std::string path)
{
    std::string wrongSep;
    if(TRAININGBOOK_FILE_SEPARATOR == "\\")
        wrongSep = "/";
    else
        wrongSep = "\\";
       
    size_t pos = path.find(wrongSep);
    while(pos != std::string::npos)
    {
        path.replace(pos, 1, TRAININGBOOK_FILE_SEPARATOR);
        pos = path.find(wrongSep);
    }    
    
    if(!FileExists(path))
        return;
    
    std::string rmCommand = std::string(TRAININGBOOK_RM_COMMAND) + " "+ path;
	std::cout << std::system(rmCommand.c_str()) << "\n";
}

///@brief Return true if file exists
///@param path Path to the file to test
bool FileExists(std::string path)
{
    std::ifstream file(path.c_str());
    if(file)
    {
            file.close();
            return true;
    }
    return false;
}

///@brief Return true if directory exists
///@param path Path to the directory to test
bool DirExists(std::string path)
{
    if(access (path.c_str(), W_OK) == 0)
        return true;
    else
        return false;
}



///@brief Convert a string to double precision value
///@param s String containing a double precision value to convert
double StringToDouble(std::string s)
{
    double res;
    std::istringstream stream(s);
    stream >> res;
    if(stream.fail())
        std::cout << "Error while converting string to double\n";
    return res;
}

///@brief Convert a string to unsigned int value
///@param s String containing a unsigned int value to convert
unsigned int StringToUInt(std::string s)
{
    double res;
    std::istringstream stream(s);
    stream >> res;
    if(stream.fail())
        std::cout << "Error while converting string to unsigned int\n";
    return res;
}

///@brief Convert a double precision value to a string
///@param val Value to convert
///@param precision Number of decimal numbers
std::string DoubleToString(double val, unsigned int precision)
{
    std::ostringstream stream;
    stream.setf(std::ios::fixed, std::ios::floatfield);
    stream.precision(precision);
    
    stream << val;
    return stream.str();
}

///@brief Convert an unsigned int value to a string
///@param val Value to convert
std::string UIntToString(unsigned int val)
{
    std::ostringstream stream;
    stream << val;
    return stream.str();
}

